package com.example.demo.Controler.UserController;

import com.example.demo.Entity.ContentTag;
import com.example.demo.Entity.User;
import com.example.demo.Entity.Video;
import com.example.demo.Service.UserService;
import com.example.demo.Service.VideoService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.*;
import static com.example.demo.Errno.ErrnoDef.ERRNO_SUCCESS;

public class UpdateVideoController {
    static private final Logger logger = LoggerFactory.getLogger(RegisterController.class);
    @Autowired
    private VideoService videoService;
    /**
     * 用于声明注册时允许导入的有效参数的json
     * 对应 User 实体的属性
     */
    static String update_videoValidAttr_str = "["
            //+ "\"id\""
            + ", \"title\""
            + ", \"intro\""
            + ", \"url\""
            + ", \"cover\""
            //+ ", \"author_id\""
            //+ ", \"create_time\""
            //+ ", \"edit_time\""
            //+ ", \"recycle_time\""
            //+ ", \"status\""
            //+ ", \"views\""
            //+ ", \"stars\""
            //+ ", \"likes\""
            + ", \"partition\""
            + "]";
    /**
     * 用于声明注册时允许导入的有效参数的列表，由json在初始化时转换而来
     * 对应 User 实体的属性
     */
    private static List<String> update_videoValidAttr;
    /**
     * 用于声明注册时允许导入的默认值的列表，由json在初始化时做转换
     * 对应 User 实体的属性，在注册前生成默认实体
     */
    static String update_videoDefaultAttr_str ="{"
            //+ "\"id\""
            //+ "  \"title\""
            //+ ", \"intro\""
            //+ ", \"url\""
            //+ ", \"cover\""
            //+ ", \"author_id\""
            //+ ", \"create_time\""
            //+ ", \"edit_time\""
            //+ ", \"recycle_time\""
            + ", \"status\":0"
            + ", \"views\":0"
            + ", \"stars\":0"
            + ", \"likes\":0"
            //+ ", \"partition\""
            + "]";

    /**
     * 该静态代码块 用于初始化声明 registerValidAttr 的有效参数的列表，由json转换
     *
     **/
    static {

        ObjectMapper objectMapper = new ObjectMapper();
        try {
            update_videoValidAttr = objectMapper.readValue(update_videoValidAttr_str, new TypeReference<List<String>>(){});
        } catch (JsonProcessingException e) {
            logger.error(e.getMessage());
        }

    }

    /**
     * 上传视频
     * @param req_map 请求对象
     * @return json串（字典），键值对：<br>
     * - errno：错误码 (0为无错误)<br>
     * - msg：错误信息 <br>
     * -
     */
    @PostMapping("api/update_video")
    public Map<String,Object>update_video(@RequestBody Map<String,Object> req_map) {
        Video updVideo = null;
        Map<String, Object> map = new HashMap<>();
        ObjectMapper objectMapper = new ObjectMapper();
        ContentTag updConTag= null;
        try {
            try {
                updVideo = objectMapper.readValue(update_videoDefaultAttr_str, Video.class);
                String tags = objectMapper.readValue("content_tag", String.class);
            } catch (JsonProcessingException e) {
                logger.error(e.getMessage());
            }
            for (String attr : update_videoValidAttr) {
                if (req_map.get(attr) != null) {
                    updVideo.setAttribute(attr, req_map.get(attr));
                } else {
                    map.put("errno", 1003);
                    map.put("msg", "请填写必填项 " + attr);
                    return map;
                }
            }
            /*
            TODO:不确定前端发送的tag字典如何处理
            String[] Tags = tags.split("");
            for(String tag: Tags)
            {
            updConTag.setAttribute("video_id",updVideo.id);
            updConTag.setAttribute("tag_name",tag);
            videoService.insertContentTag(updConTag);
            }*/
            videoService.insertVideo(updVideo);
            map.put("errno", ERRNO_SUCCESS.getErrno());
            map.put("msg", "上传成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("errno",1002);
            map.put("msg","上传失败");
        }
        return map;
    }
}
